package com.example.xianji.utils;

import java.util.List;

import android.graphics.Bitmap;
import android.util.Log;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageSize;
import com.nostra13.universalimageloader.core.imageaware.ImageViewAware;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.nostra13.universalimageloader.utils.ImageSizeUtils;
import com.nostra13.universalimageloader.utils.MemoryCacheUtils;
/**
 * ImageLoader 加载图片列表避免闪烁，重复
 * @author me
 *
 */
public class ImageLoadUtil {

	//清除动画ImageView和Bitmap手动从内存缓存设置            ImageLoader 加载图片时避免item复用时的图片重复，闪烁
		public static void loadImageByPath(DisplayImageOptions options, String path, ImageViewAware image, ImageLoadingListener listener) {
	        ImageLoader.getInstance().cancelDisplayTask(image);
	        image.getWrappedView().clearAnimation();
	        image.setImageDrawable(null);

	        final ImageSize size = ImageSizeUtils.defineTargetSizeForView(image, null);
	        final String cacheKey = MemoryCacheUtils.generateKey(path, size);
	        final List<Bitmap> cachedBitmaps = MemoryCacheUtils.findCachedBitmapsForImageUri(cacheKey, ImageLoader.getInstance().getMemoryCache());

	        if (cachedBitmaps.size() > 0) {
	            final Bitmap bitmap = cachedBitmaps.get(0); 

	            // Yep, sometimes it is null
	            if (bitmap != null) {
	                if (listener != null) {
	                    listener.onLoadingComplete(path, image.getWrappedView(), bitmap);
	                }

	                image.setImageBitmap(bitmap);
	                Log.i("ssssaaaaaa", cachedBitmaps.size()+"内存加载");

	                return;
	            }
	        }
	        Log.i("ssssaaaaaa", "网络加载网络机爱在");
	        ImageLoader.getInstance().displayImage(path, image, options, listener);
	    }
}
